package org.example.wu_and_dian.controller;

import org.example.wu_and_dian.annotation.TokenRequired;
import org.example.wu_and_dian.dto.response.AjaxResult;
import org.example.wu_and_dian.entity.EntityClass;
import org.example.wu_and_dian.service.EntityClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class EntityClassController {
    @Autowired
    private EntityClassService entityClassService;
    @TokenRequired
    @GetMapping("/get_entity_classes/{documentId}")
    public AjaxResult getEntityClassByDocumentId(@PathVariable("documentId") String documentId) {
        List<EntityClass> entityClasses =  entityClassService.getByDocumentId(documentId);
        return AjaxResult.success("获取实体类成功", entityClasses);

    }
    @TokenRequired
    @PostMapping("/import_entity_class")
    public AjaxResult importEntityClasses(@RequestBody  Map<String,List<EntityClass>> request) {
        List<EntityClass> entityClasses = request.get("entityClasses");
        if (entityClasses == null || entityClasses.isEmpty()) {
            return AjaxResult.success("导入实体类成功,因为没给实体类");
        }
        try {
            return entityClassService.batchImportEntityClasses(entityClasses);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    @TokenRequired
    @PostMapping("/add_entity_class")
    public AjaxResult addEntityClass(@RequestBody EntityClass entityClass) {
        return entityClassService.insertEntityClass(entityClass);
    }
    @TokenRequired
    @PutMapping("/update_entity_class")
    public AjaxResult updateEntityClass(@RequestBody Map<String, EntityClass> requests) {
        EntityClass entityClass = requests.get("entityClasses");
        return entityClassService.updateEntityClass(entityClass);
    }
    @TokenRequired
    @PostMapping("/delete_entity_class")
    public AjaxResult deleteEntityClass(@RequestBody Map<String,String> requests) {
        try {
            String documentId = requests.get("documentId");
            String entityClassId = requests.get("entityId");
            EntityClass entityClass = new EntityClass();
            entityClass.setId(entityClassId);
            entityClass.setDocumentId(documentId);
            return entityClassService.deleteEntityClass(entityClass);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}
